package com.stedu.service.impl;

import com.stedu.bean.Owner;
import com.stedu.exception.SteduException;
import com.stedu.mapper.OwnerMapper;
import com.stedu.service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OwnerServiceImpl implements OwnerService {
    @Autowired
    private OwnerMapper ownerMapper;


    @Override
    public boolean insertBatch(List<Owner> owners) throws SteduException {
            try
            {
                int result = ownerMapper.insertBatch(owners);
                return result == owners.size();
            } catch (Exception e) {
                //throw new SteduException("批量插入失败");
            }
            return false;
    }

    @Override
    public List<Owner> selectByOname(String oname,String ophone) throws SteduException {
        return ownerMapper.selectByOname(oname,ophone);
    }

    @Override
    public boolean insert(Owner o) throws SteduException {
        if(ownerMapper.selectByOid(o.getOid()) !=null){
            throw new SteduException("该业主已经存在，无法重复添加");
        }
        return ownerMapper.insert(o) == 1;
    }

    @Override
    public boolean delete(Integer oid) throws SteduException {
        if (ownerMapper.selectByOid(oid) ==null){
            throw new SteduException("该业主不存在，无法删除");

        }
        return ownerMapper.delete(oid)==1;
    }

    @Override
    public boolean update(Owner o) throws SteduException {
        if (ownerMapper.selectByOid(o.getOid()) == null){
            throw new SteduException("该报修任务不存在，无法修改");
        }
        return ownerMapper.update(o) == 1;
    }

    @Override
    public Owner selectByOid(Integer Oid) {
        return ownerMapper.selectByOid(Oid);
    }

    @Override
    public List<Owner> selectAll() {
        return ownerMapper.selectAll();
    }
}
